package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.Planunit;
import com.woniuxy.factory.entity.ProductionPlan;
import com.woniuxy.factory.entity.WorkOrder;
import com.woniuxy.factory.mapper.MissionMapper;
import com.woniuxy.factory.service.MissionService;
import com.woniuxy.factory.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MissionServiceImp implements MissionService {

    @Autowired(required = false)
    MissionMapper mm;
    @Autowired
    WorkOrderService wos;

    //查询所有的计划单
    @Override
    public List<ProductionPlan> selectMission(ProductionPlan productionPlan,int page,int limit) {
        page = (page-1)*limit;//设置初始索引
        //查询已通过申请的计划单
        List<ProductionPlan> pp = mm.selectMission(productionPlan,page,limit);
        //查询已派单的计划单
        List<WorkOrder> lwo = wos.selectAllWorkOrder();
        //循环判断计划单是否已经派单
        for(ProductionPlan p : pp){
            boolean isGoIn = false;
            for(WorkOrder w : lwo){
                if(p.getPrpId().equals(w.getProductionPlan().getPrpId())){
                    p.setPlanState("已派单");
                    isGoIn = true;
                    break;
                }
            }
            if(isGoIn==false){
                p.setPlanState("未派单");
            }
        }
        return pp;
    }

    //查询所有的计划单的数目
    @Override
    public int selectMissionSum(ProductionPlan productionPlan) {
        int count = mm.selectMissionSum(productionPlan);
        return count;
    }

    //查询当前计划单中的所有计划项
    @Override
    public List<Planunit> selectPlanunit(String id,int page,int limit) {
        page = (page-1)*limit;//设置初始索引
        List<Planunit> lp = mm.selectPlanunit(id,page,limit);
        return lp;
    }

    //查询当前计划单中的所有计划项的总数
    @Override
    public int selectPlanunitSum(String id) {
        int count = mm.selectPlanunitSum(id);
        return count;
    }

}
